Imports System.Xml
Public Partial Class projekt_erstellen_zwei
    Inherits System.Web.UI.Page
    Private kundenstring As String
    Private db As hc_databaseDataContext


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        db = New hc_databaseDataContext
        Try

            kundenstring = Session("Kundenstring")
        Finally
        End Try

        Dim var = From it In db.Ansprechpartners _
        Where it.Kundes.Name = kundenstring _
        And it.Vorname IsNot "" _
       Distinct Select New String(it.Vorname + " " + it.Nachname)

        For Each s As String In var

            dd_ansprechpartner.Items.Add(s)

        Next



    End Sub

    Protected Sub chb_neuerAnsprechpartner_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chb_neuerAnsprechpartner.CheckedChanged
        If chb_neuerAnsprechpartner.Checked = True Then
            mv_neuerAnsprechPartner.ActiveViewIndex = 0
            dd_ansprechpartner.Items.Clear()
        Else
            mv_neuerAnsprechPartner.ActiveViewIndex = 1

        End If

    End Sub

    Protected Sub btn_weiter_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_weiter.Click
        If chb_neuerAnsprechpartner.Checked = True Then


            db = New hc_databaseDataContext
            Dim ansprechpartner1 As Ansprechpartner = New Ansprechpartner
            ansprechpartner1.Vorname = txt_vorname.Text
            ansprechpartner1.Nachname = txt_nachname.Text
            ansprechpartner1.Telefonnummer = txt_tele.Text
            ansprechpartner1.Fax = txt_fax.Text

            Dim var = (From it In db.Kundes _
                      Where it.Name = kundenstring _
                      Select it).Single


            var.Ansprechpartners.Add(ansprechpartner1)


            db.Ansprechpartners.InsertOnSubmit(ansprechpartner1)

            db.SubmitChanges()

            Dim str As String = "projekt_erstellen_drei.aspx?Kunde=" + var.ID.ToString + "&Ansprechpartner=" + ansprechpartner1.ID.ToString
            Response.Redirect(str)

        Else
            Dim funktionen As New stored_functions
            Dim var = (From it In db.Kundes _
                      Where it.Name = kundenstring _
                      Select it).Single
            Dim var2 = (From it In db.Ansprechpartners _
                       Where it.Vorname = funktionen.stringInVorundNachnameZerlegen(dd_ansprechpartner.SelectedItem.Text)(0).ToString _
                       And it.Nachname = funktionen.stringInVorundNachnameZerlegen(dd_ansprechpartner.SelectedItem.Text)(1).ToString _
                       And it.Kunde_id = var.ID _
                       Select it.ID).Single
            Dim str As String = "projekt_erstellen_drei.aspx?Kunde=" + var.ID.ToString + "&Ansprechpartner=" + var2.ToString
            Response.Redirect(str)

        End If


    End Sub

    Protected Sub txt_fax_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txt_fax.TextChanged
        
    End Sub

    Protected Sub txt_nachname_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txt_nachname.TextChanged

    End Sub

    Protected Sub dd_ansprechpartner_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dd_ansprechpartner.SelectedIndexChanged

    End Sub
End Class